package com.orong.adapter;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import com.orong.Constant;
import com.orong.R;
import com.orong.entity.LoanSummary;
import com.orong.utils.FileUitls;
import com.orong.utils.LoadImageRespone;
import com.orong.utils.net.NetUtils;
import com.orong.utils.net.NetUtils.DownloadCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @Title: ProjectsAdapter.java
 * @Description: 展示可投资项目的列表
 * @author lanhaizhong
 * @date 2013年7月11日下午5:24:29
 * @version V1.0 Copyright (c) 2013 Company,Inc. All Rights Reserved.
 */
public class ProjectsAdapter extends BaseAdapter {
	private List<LoanSummary> projectList;
	private HashMap<String, SoftReference<Bitmap>> pictureMap;
	private Context context;

	public ProjectsAdapter(List<LoanSummary> projectList, Context context, HashMap<String, SoftReference<Bitmap>> pictureMap) {
		this.projectList = projectList;
		this.context = context;
		this.pictureMap = pictureMap;
	}

	@Override
	public int getCount() {
		return projectList == null ? 0 : projectList.size();
	}

	@Override
	public Object getItem(int position) {
		return projectList == null ? null : projectList.get(position-1);//因为有head占了一个条目
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.projects_item, null);
			holder = new ViewHolder();
			holder.iv_project_ic = (ImageView) view.findViewById(R.id.iv_project_ic);
			holder.tv_title = (TextView) view.findViewById(R.id.tv_project_theme);
			holder.tv_sum = (TextView) view.findViewById(R.id.tv_project_sum);
			holder.tv_progress = (TextView) view.findViewById(R.id.tv_project_progress);
			holder.tv_deadline = (TextView) view.findViewById(R.id.tv_deadline);
			holder.tv_rate = (TextView) view.findViewById(R.id.tv_rate);
			//holder.deviderView = view.findViewById(R.id.devider_view);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		LoanSummary summary = projectList.get(position);
		holder.iv_project_ic.setImageResource(R.drawable.defaultimage);
		getOrLoadImage(summary.getPicture(), holder.iv_project_ic);
		holder.tv_title.setText(summary.getLoanName());
		holder.tv_sum.setText(String.format(context.getString(R.string.float_format), summary.getMoney()));
		holder.tv_progress.setText(String.format(context.getString(R.string.schedule), summary.getSchedule()) + "%");
		setday2date(summary.getDeadline(), holder.tv_deadline);
		holder.tv_rate.setText(String.format(context.getString(R.string.interest_rate), summary.getInterestRate()) + "%");
		return view;
	}

	static class ViewHolder {
		ImageView iv_project_ic;
		TextView tv_title;// 项目名称
		TextView tv_sum;// 金额
		TextView tv_progress;// 进度
		TextView tv_deadline; // 期限
		TextView tv_rate;// 利率
	//	View deviderView;// 分割先

	}

	public void setProjectList(List<LoanSummary> projectList) {
		this.projectList = projectList;
	}

	/**
	 * 天数变成月日形式
	 * 
	 * @param day
	 * @param view
	 */
	private void setday2date(int day, TextView view) {
		Calendar calendar = Calendar.getInstance();
		int oldYear;
		int oldMonth;
		int oldDay;
		oldYear = calendar.get(Calendar.YEAR);
		oldMonth = calendar.get(Calendar.MONTH);
		oldDay = calendar.get(Calendar.DAY_OF_MONTH);

		calendar.add(Calendar.DAY_OF_MONTH, day);
		int newYear;
		int newMonth;
		int newDay;
		newYear = calendar.get(Calendar.YEAR);
		newMonth = calendar.get(Calendar.MONTH);
		newDay = calendar.get(Calendar.DAY_OF_MONTH);

		int tempday = 0;
		tempday = newDay - oldDay;
		if (tempday < 0) {
			if (newMonth != 0) {
				newMonth--;
				calendar.set(Calendar.MONTH, newMonth);
			} else {
				newYear--;
				calendar.set(Calendar.YEAR, newYear);
				calendar.set(Calendar.MONTH, 11);
			}
			int days = calendar.getMaximum(Calendar.DATE);
			tempday = tempday + days;
		}
		int tempMonth = (newYear - oldYear) * 12 + newMonth - oldMonth;
		view.setText(String.format(context.getString(R.string.deadline), tempMonth, tempday));
	}

	/**
	 * 加载图片
	 * 
	 * @param url
	 * @param chageview
	 */
	private void getOrLoadImage(String url, final ImageView chageview) {
		if (url == null || "null".equalsIgnoreCase(url)) {
			return;
		}
		final String key = url.substring(url.lastIndexOf('/') + 1);
		SoftReference<Bitmap> softReference = pictureMap.get(key);
		if (softReference != null) {
			chageview.setImageBitmap(softReference.get());
			notifyDataSetChanged();
		} else {
			File file = new File(FileUitls.getSDCardPath(context) + "/" + Constant.PROJECTIMG + "/" + key);
			if (file.exists()) {
				Bitmap bitmap = FileUitls.getBitmapfromSDPathByName(context, Constant.PROJECTIMG + "/" + key);
				if (bitmap != null) {
					softReference = new SoftReference<Bitmap>(bitmap);
					pictureMap.put(key, softReference);
					chageview.setImageBitmap(bitmap);
					notifyDataSetChanged();
				}
			} else {
				NetUtils.downLoadImage(url, context, Constant.PROJECTIMG, new DownloadCallback() {

					@Override
					public void loadCompleteCallback(LoadImageRespone respone) {
						chageview.setImageBitmap(respone.getBitmap());
						SoftReference<Bitmap> softReference = new SoftReference<Bitmap>(respone.getBitmap());
						pictureMap.put(key, softReference);
						notifyDataSetChanged();
					}

					@Override
					public void beforeDownload() {
						// TODO Auto-generated method stub

					}
				});
			}
		}
	}
}
